package me.helllp.dake.generator.parser;

import com.github.javaparser.JavaParser;
import com.github.javaparser.JavaParserAdapter;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import me.helllp.dake.generator.meta.core.*;

public class TransferToParser {
    public static Parameter transfer(JavaParser parser, SmartParameter smartParameter) {
        return JavaParserAdapter.of(parser).parseParameter(smartParameter.builder().build().toString());
    }

    /**
     * 基于JavaPoet的字段转换为JavaParser的字段类型
     *
     * @param smartField 字段类型
     * @return 字段类型
     */
    public static FieldDeclaration transfer(SmartField smartField) {
        FieldDeclaration field = new FieldDeclaration();
        field.setModifiers(Modifier.Keyword.PRIVATE);
        field.setBlockComment(smartField.getDesc());
        field.setVariables(new NodeList<>());
        VariableDeclarator declarator = new VariableDeclarator();
        declarator.setName(smartField.getName());
        declarator.setType(smartField.getType().build().toString());
        field.getVariables().add(declarator);
        return field;
    }

    public static AnnotationExpr transfer(JavaParser parser, SmartAnnotation smartAnnotation) {
        return JavaParserAdapter.of(parser).parseAnnotation(smartAnnotation.builder().build().toString());
    }

    public static MethodDeclaration transfer(JavaParser parser, SmartMethod smartMethod) {
        return JavaParserAdapter.of(parser).parseMethodDeclaration(smartMethod.builder().build().toString());
    }

    public static ClassOrInterfaceType transfer(JavaParser parser, SmartClass smartClass) {
        return JavaParserAdapter.of(parser).parseClassOrInterfaceType(smartClass.builder().build().toString());
    }

    public static ClassOrInterfaceType transfer(JavaParser parser, SmartEnum smartEnum) {
        return JavaParserAdapter.of(parser).parseClassOrInterfaceType(smartEnum.builder().build().toString());
    }

    public static ClassOrInterfaceType transfer(JavaParser parser, SmartInterface smartInterface) {
        return JavaParserAdapter.of(parser).parseClassOrInterfaceType(smartInterface.builder().build().toString());
    }
}
